package com.qf.controller;

import com.qf.pojo.DtsStorage;
import com.qf.service.DtsStorageService;
import com.qf.util.ResponseUtil;
import com.qf.util.UUID;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * @author bss
 * @version V1.0
 * @Project maven
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/10/17 14:08
 */

@RestController
@RequestMapping("/admin/storage")
public class AdminStorageController {
    @Autowired
    DtsStorageService storageService;

//    admin/storage/list?page=1&limit=20&sort=add_time&order=desc
    @GetMapping("/list")
    public Object list(@RequestParam(value = "page",required = false, defaultValue = "1") Integer page,
                       @RequestParam(value = "limit",required = false, defaultValue = "20")Integer limit,
                       @RequestParam(value = "sort",required = false, defaultValue = "add_time")String sort,
                       @RequestParam(value = "order",required = false, defaultValue = "desc")String order,
                       String key,String name){

        Map<String, Object> data = storageService.selectByPage(page,limit,sort,order,key,name);
        return ResponseUtil.ok(data);
    }
//    上传文件到oss
//    并存储到数据库
    @PostMapping("/create")
    public Object create(MultipartFile file) throws Exception {
//        原始文件名
        String filename = file.getOriginalFilename();
//        文件大小
        long size = file.getSize();
//        图片类型
        String type = file.getContentType();
//        唯一的名称
        String uuid = UUID.randomUUID().toString();
        String substring = filename.substring(filename.lastIndexOf("."));
        String name = uuid+substring;

        DtsStorage storage = storageService.uploadFile(name, (int) size,type,filename,file.getBytes());

        return ResponseUtil.ok(storage);
    }

    @PostMapping("/delete")
    public Object delete(@RequestBody DtsStorage storage){
        storageService.delete(storage);
        return ResponseUtil.ok();
    }

    @PostMapping("/update")
    public Object update(){


        return ResponseUtil.ok();
    }
}
